export class PurchaseTable { // 多供进价表格
  constructor ({ list, columns, loading } = {}) {
    this.list = list || []; // 当前展示表格数据
    this.columns = columns || []; // 表列
    this.loading = loading || false; // 表格加载loading
    this.unifiedList = []; // 初始默认表格数据（只有重新关联数据才会变更），统一供价表格同此
    this.areaList = []; // 区域供价表格数据
  }
  // 开始加载
  startLoading () {
    this.loading = true;
  }
  // 停止加载
  stopLoading () {
    this.loading = false;
  }
  // 根据状态变化loading
  changeLoading (state) {
    this.loading = state;
  }
  // 设置当前展示表格数据
  setList (list) {
    this.list = _.cloneDeep(list);
  }
  // 设置统一进价表格数据
  setUnified (list) {
    this.unifiedList = _.cloneDeep(list.map(item => ({ ...item, purchasePriceType: '1' })));
  }
  // 设置区域进价表格数据
  setArea (list) {
    this.areaList = _.cloneDeep(list.map(item => ({ ...item, purchasePriceType: '2' })));
  }
  // 重置当前展示表格数据（即展示为统一进价表格数据）
  resetList () {
    this.setList(this.unifiedList);
  }
  // 根据供货价格方式设置当前展示表格数据
  setListByType (isAreaPurchase) {
    if (isAreaPurchase) { // 是区域进价
      this.setList(this.areaList);
    } else { // 是统一进价
      this.setList(this.unifiedList);
    }
  }
  // 设置表列
  setColumns (columns) {
    this.columns = _.cloneDeep(columns);
  }
  // 获取表格长度
  getSize (type) {
    const key = type ? `${type}List` : 'list';
    return this[key]?.length || 0;
  }
  // 表格为空
  isNull () {
    return this.getSize() === 0;
  }
  // 清空所有表格
  clearAll () {
    this.list = [];
    this.unifiedList = [];
    this.areaList = [];
  }
  // 初始化表格
  initAll (list, initList) {
    this.setList(list);
    this.setUnified((initList || list).map(item => ({ ...item, purchasePriceType: '1' })));
    this.setArea(list.map(item => ({ ...item, purchasePriceType: '2' })));
  }
}
